
/*
 * @Descripttion: 音效管理
 * @Author: xutao
 * @Date: 2020-01-16 21:40:56
 * @LastEditors  : xutao
 * @LastEditTime : 2020-01-19 20:46:54
 */
export class AudioMap {
    /**
     * 背景音乐
     */
    public static BG: string = '';
    /**
     * 合成音效
     */
    public static MERGE_SOUND: string = '';

    /**
     * 胜利音效
     */
    public static VIC_SOUND: string = '';
}
export class AudioMgr {

    private static _inst: AudioMgr;

    public static get inst(): AudioMgr {
        if (!this._inst) {
            this._inst = new AudioMgr();
        }
        return this._inst;
    }

    private $audioPool: HTMLAudioElement[] = [];
    public $audioMap = new Map<string, HTMLAudioElement>();

    private getAudio(url: string) {
        let audio: HTMLAudioElement = this.$audioMap.get(url);
        if(audio){
            return audio;
        }
        
        if (this.$audioPool.length > 0) {
            audio = this.$audioPool.pop();  // 从对象池取
        } else {
            audio = document.createElement('audio') as HTMLAudioElement;
            audio.preload = 'preload';
            audio.addEventListener('ended', () => {
                //this.$audioPool.push(audio); // 播放完加入对象池
                //this.$audioMap.delete(url);
            });
            audio.addEventListener('pause', () => {
                //this.$audioPool.push(audio); // 播放暂停加入对象池
                //this.$audioMap.delete(url);
            });
            cc.game.container.appendChild(audio); // DOM常驻

        }
        audio.src = url;
        // if (audio.src != url) {
        //     audio.src = url;
        // }
        this.$audioMap.set(url, audio);
        return audio;
    }

    /**
     * 播放音效
     * @param url 
     */
    public playEffect(url: string) {
        if (url == undefined) {
            cc.warn('@xts->AudioMgr playEffect url为空');
            return;
        }
        let audio = this.getAudio(url);
        audio.loop = false;
        audio.play();
        cc.log('@xts->AudioMgr playEffect: ', url);
    }

    /**
     * 预加载音效
     */
    public preloadEffect(url: string):void{
        if (url == undefined) {
            cc.warn('@xts->AudioMgr playEffect url为空');
            return;
        }
        let audio = this.getAudio(url);
    }

    /**
     * 播放音乐
     */
    public playMusic(url: string) {
        try {
            let audio = this.$audioMap.get(url);
            audio.loop = true;
            audio.play();
            cc.log('@xts->AudioMgr playMusic: ', url);
        } catch (e) {
            cc.error('@xts->AudioMgr playMusic error: ', e);
        }
    }

    /**
     * 停止音乐
     * @param url 
     */
    public stopMusic(url: string) {
        try {
            let audio = this.$audioMap.get(url);
            cc.log('@xts->AudioMgr stopMusic: ', url);
            audio.pause();
        } catch (e) {
            cc.error('@xts->AudioMgr stopMusic error: ', e);
        }

    }


    /**
     * 
     */
    public playSoundLocal(soundUrl: string): void {
        soundUrl = "sound/"+soundUrl;
        cc.loader.loadRes(soundUrl, cc.AudioClip,(err, clip)=>{
            var audioID = cc.audioEngine.playEffect(clip, false);
        });
    }


    /**
     * 
     */
    public bgm:number;
    public playBgm(soundUrl: string): void {
        soundUrl = "sound/"+soundUrl;
        cc.loader.loadRes(soundUrl, cc.AudioClip,(err, clip)=>{
            this.bgm = cc.audioEngine.playEffect(clip, true);
        });
    }

    public stopBgm():void{
        cc.audioEngine.stopEffect(this.bgm);
    }
}
